<?php

class AppSideBar {

    public $root;

    public function showHtml(){
        echo
        '
         <div class="sidebar">

        	
                <div class="menu">'.
                      $this->showMenu()
                    .'
                </div>
             
                <div class="menu">'.
                      $this->showtree()
                    .'
                </div>

           <!-- <div class="menu">
                <ul>
                    <li><a href="http://www.templatemo.com" target="_parent" class="current">Home</a></li>
                    <li><a href="http://www.webdesignmo.com" target="_parent">Web Design</a></li>
                    <li><a href="http://www.flashmo.com/page/1" target="_parent">Flash Files</a></li>
                    <li><a href="http://www.templatemo.com/page/1" target="_parent">Website Templates</a></li>
                    <li><a href="http://www.templatemo.com/page/2" target="_parent">Our Company</a></li>
                    <li><a href="http://www.templatemo.com/page/3" target="_parent">Contact Information</a></li>
              </ul>-->
            </div> <!-- end of menu -->
                <!--
                <div class="sidebar_box">
            	<div class="sb_title">Client Login</div>
                <div class="sb_content">
                	<div class="login_form">
                        <form method="post" action="#">
                        	<p><span>User Name:</span>
                            <input type="text" class="username" name="username" class="login_input" />
                            </p>
                            <p><span>Password:</span>
                            <input type="password" class="password" name="password" class="login_input" />
                            </p>
                            <input type="submit" name="submit" class="login_submit" value=" " />
                        </form>
					</div>
                </div>
                <div class="sb_bottom"></div>
            </div>

            <div class="sidebar_box">
            	<div class="sb_title">Latest Updates</div>
                <div class="sb_content">

                	<div class="sb_news_box">
						<a href="#">Maecenas adipiscing elem sum ipsum.</a>
                        <span>25 September 2048</span>
                    </div>

                    <div class="sb_news_box">
						<a href="#">Aser ecenas adipiscing de lorem ipsum.</a>
                        <span>18 September 2048</span>
                    </div>

                    <a href="#"><strong>View All</strong></a>
               </div>

              <div class="sb_bottom"></div>

            </div>
           
                <center>
                <a href="http://validator.w3.org/check?uri=referer"><img style="border:0;width:88px;height:31px" src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" width="88" height="31" vspace="8" border="0" /></a> &nbsp;&nbsp;&nbsp;
                <a href="http://jigsaw.w3.org/css-validator/check/referer"><img style="border:0;width:88px;height:31px"  src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" vspace="8" border="0" /></a>
                    </center>

                <div class="cleaner"></div>
            </div> <!-- end of sidebar -->
        ';
    }

     public function showMenu(){
        $string = '<ul>';
        $array = array( 1 => 'Cadastrar-se',
                        2 => 'Autenticar-se');
        foreach ($array as $key=>$item){
            if($key==Config::$option){
                $string = $string.'<li><a href="'.Config::$domainName.'/Application.php?option='.$key.'" class="current">'.$item.'</a></li>';
            }else{
            $string = $string.'<li><a href="'.Config::$domainName.'/Application.php?option='.$key.'">'.$item.'</a></li>';
            }
        }

        $string = $string.'</ul>';

        return $string;
    }

     public function showtree(){

      //$reinoAnimalia = $nodeDao->getNodeById(2);
       //$this->initLista($reinoAnimalia);

        require_once('DAO/mySqlConnect.php'); // create the mySqlConnect class
        $mySqlConnect = new MySqlConnect();
        $current = NodeDao::db_node_by_id($mySqlConnect->getLinkDB(), Config::$current);


        $this->initLista($current);

        $mySqlConnect->closeDB();
        return $this->showArrayObject();

    }


   public $matriz;
    public $object;
    public $cont=0;
    public $flag = true;

    public function  initLista($node){
        $cont = 0;
        $this->object = new node(null,null,null);
        $this->listar($node);

    }


    public function listar($node){

        $pai = $node->father;
        if($pai==null){
            $this->object->append($node);
            return;
        }else{
           if($this->flag){
                $iterator = $node->getIterator();

                $i=0;
                $array = array();

                  while($iterator->valid()) {
                   $array[$i] =  $iterator->current();
                   $i=$i+1;
                   $iterator->next();
               }
               if(count($array)>0){
                $this->object->append($array);
               }
                $this->flag=false;
                $this->object->append($node);
            }else{
                $this->object->append($node);
            }

            $this->cont++;
            $mySqlConnect = new MySqlConnect();
            $rec = NodeDao::db_node_by_id($mySqlConnect->getLinkDB(), $pai->id);
            if($rec!=null){
                $this->listar($rec);
            }

            $mySqlConnect->closeDB();

        }
    }

    public function getTab($int){
        return $int*10;
    }



    public function showArrayObject(){

        // include 'phpClasses/Config.php';

         $iterator = $this->object->getIterator();
         $array;
         $x=0;


               $string ='';

                while($iterator->valid()) {
                    $array[$x] = $iterator->current();
                    $x++;
                   $iterator->next();
                }

                $array = array_reverse($array, false);

                $i=0;

                //impressao do menu
                foreach ($array as $no){
                    if(is_array($no)){
                        $string = $string.'<ul>';
                        foreach ($no as $irmao) {
                            if($irmao->id==Config::$current){
                                $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a class="current" href ="'.Config::$domainName.'/Application.php?current='.$irmao->id.'">'. $irmao->taxo->taxo.' '.$irmao->taxo->nome.'</a></li>';
                            }else{
                                $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a href ="'.Config::$domainName.'/Application.php?current='.$irmao->id.'">'. $irmao->taxo->taxo.' '.$irmao->taxo->nome.'</a></li>';
                            }
                        }
                         $string = $string.'</ul>';
                    }else{
                   $string = $string.'<ul>';
                    if($no->id==Config::$current){
                         $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a class="current" href ="'.Config::$domainName.'/Application.php?current='.$no->id.'">'. $no->taxo->taxo.' '.$no->taxo->nome .'</a></li>';
                    }else{
                    $string = $string.'<li style="margin-left:'.$this->getTab($i).'px;"><a href ="'.Config::$domainName.'/Application.php?current='.$no->id.'">'. $no->taxo->taxo.' '.$no->taxo->nome .'</a></li>';
                    }
                    }
                    $i++;
                }

                for ($x=0;$x<(count($array)-1);$x++){
                     $string = $string.'</ul>';
                }

                return $string;
    }

  

}
?>

